#include "secondlevelwidget.h"

#include "doctorwidget.h"
#include "config.h"

#include <fstream>

using namespace  std;

SecondLevelWidget::SecondLevelWidget(std::string firstItem)
{
    cout<<"firstItem:"<<firstItem<<endl;
    initTextData(firstItem);
}

int SecondLevelWidget::initTextData(string firstItem)
{
    fstream file;
    file.open(firstItem,std::ios_base::in);
    if(!file.is_open())
    {
        std::cout<<"open "<<firstItem<<"error "<<strerror(errno)<<std::endl;
        return -1;
    }

    while(!file.eof())
    {
        char buf[1024] = {0};
        file.getline(buf,sizeof(buf));

        //eg: 儿科=儿科综合/小儿内科/小儿外科/新生儿科/儿童营养保健科

        //切割一级学科   eg:儿科
        std::string text = std::string(buf);
        std::string value = text.substr(0,text.find("/"));

        m_list.push_back(Button(value.c_str(),
                                   posX+(spaceWidth+perWidth)*column,
                                   posY+(spaceHeight+perHeight)*line,
                                   perWidth,
                                   perHeight));

        if(++cnt % 4 == 0)
            line++;

        column = (column+1) %4;

        std::cout<<value<<std::endl;
    }

    file.close();
}

int SecondLevelWidget::exec()
{
    int state;
    bool exitFlag = false;

    Bmp b1("./res/secondlevel.bmp");
    b1.show();

    while(1)
    {
        Touch touch;
        int x,y;
        touch.wait(x,y);

        if(x>=0 && x<=180 && y>=50 && y<=390)//选择二级学科
            state = Mode_Select_Item;
        else if(x>=0 && x<=90 && y>=390 && y<=450)// 上翻
            state = Mode_Prev_Page;
        else if(x>=90 && x<=180 && y>=390 && y<=450)// 下翻
            state = Mode_Next_Page;
        else if(x>650 && x<800 && y>410 && y<480) //返回
             break;

        switch (state) {
            case Mode_Select_Item://选择二级学科
                {
                    DoctorWidget doctor;
                    if(doctor.exec() == Btn_OK)
                        return Btn_OK;
                }
                break;
            case Mode_Prev_Page://上翻
                break;
            case Mode_Next_Page://下翻
                break;
        }
    }
}
